package com.smart.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.smart.model.Admin;

//主要是对访问的url，进行判断是否有权限访问
public class AuthorityFilter implements Filter{
	 private FilterConfig config=null;
	 private String Webroot=null;
	 private HashMap<String,List<Integer>> hm=null;
	 private List<String> rules=null; 
	 //判断是否可以通过 
	 public boolean canPass(String uri,int type)
	 {
		 boolean res=true;
		 if(hm.containsKey(uri))
		 {
			 res=false;
			 List<Integer> list=hm.get(uri);
			 //遍历，看看有没有权限访问
			 for(int x:list)
			 {
				 if(type==x) res=true;
			 }
		 }
		 return res;
	 }
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		 this.config=filterConfig;
		 System.out.println("hello AuthorityFilter init method");
		 Webroot=config.getServletContext().getContextPath();
		 hm=new HashMap<>();
		 //对于每个页面进行增加权限
		 //业主管理
		 String url_authentication_html=Webroot+"/views/pc/authentication.html";
		 List<Integer> list_authentication_html=new ArrayList<>();
		 list_authentication_html.add(0);
		 list_authentication_html.add(2);
		 hm.put(url_authentication_html, list_authentication_html);
		 //快递管理
		 String url_delivery_search_html=Webroot+"/views/pc/delivery-search.html";
		 List<Integer> list_delivery_search_html=new ArrayList<>();
		 list_delivery_search_html.add(1);
		 list_delivery_search_html.add(0);
		 list_delivery_search_html.add(2);
		 hm.put(url_delivery_search_html, list_delivery_search_html);
		 //缴费通知
		 String url_send_fare_htmll=Webroot+"/views/pc/send-fare.html";
		 List<Integer> list_send_fare_html=new ArrayList<>();
		 list_send_fare_html.add(0);
		 list_send_fare_html.add(2);
		 hm.put(url_send_fare_htmll, list_send_fare_html);
		 //物业公告
		 String url_send_community=Webroot+"/views/pc/send-community.html";
		 List<Integer> list_send_community_html=new ArrayList<>();
		 list_send_community_html.add(0);
		 list_send_community_html.add(2);
		 hm.put(url_send_community, list_send_community_html);
		 //报修
		 String url_repair_html=Webroot+"/views/pc/repair.html";
		 List<Integer> list_repair_html=new ArrayList<>();
		 list_repair_html.add(0);
		 list_repair_html.add(2);
		 hm.put(url_repair_html, list_repair_html);
		 //用户管理
		 String url_user_html=Webroot+"/views/pc/user.html";
		 List<Integer> list_user_html=new ArrayList<>();
		 list_user_html.add(2);
		 hm.put(url_user_html, list_user_html);
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		 HttpServletRequest req=(HttpServletRequest)request;
		 HttpServletResponse resp=(HttpServletResponse)response;
		 HttpSession session=req.getSession(false);
//		 System.out.println("Webroot:"+Webroot);
		 String uri=req.getRequestURI();
		 System.out.println("request uri:"+uri);
		 if(session==null)
		 {
			//还没有登录，那就不用判断
			 System.out.println("uri:"+uri+"放过去");
			 chain.doFilter(req,resp);
		 }
		 else
         {
			 System.out.println("session is not null");
			 String str=Webroot+"/views/wechat/.*";
			 if(Pattern.matches(str,uri))
			 {
			  Admin admin=(Admin)session.getAttribute("admin");
        	  int type=admin.getType();
        	  if(canPass(uri,type))
    		  {
    		   System.out.println("uri:"+uri+"放过去");
    		   chain.doFilter(req,resp);
    		  }
    		  else
    		  {
    			 //如果没有权限访问则返回主页
    			 resp.sendRedirect("/SmartCommunity/views/pc/smart.html");
    		  }
			 }
			 else 
			 {
				 System.out.println("uri:"+uri+"放过去");
	    		  chain.doFilter(req,resp);
			 }
         }
	}

	@Override
	public void destroy() {
		
	}
  
}
